package cn.tedu.boot52.controller;

import cn.tedu.boot52.entity.Comment;
import cn.tedu.boot52.entity.User;
import cn.tedu.boot52.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {
    @Autowired(required = false)
    CommentMapper mapper;

    @RequestMapping("/comment")
    public int comment (Comment comment, HttpSession session){
        System.out.println("comment = " + comment + ", session = " + session);
        //取出session里面的user对象 判断是否登陆过
        User user = (User) session.getAttribute("user");
        if (user==null){
            return 2;//未登录
        }
        comment.setAuthor(user.getNick());
        comment.setCreated(new Date());


        mapper.insert(comment);

        return 1;
    }
    @RequestMapping("/comment/selectByWeiboId")
    public List<Comment> selectByWeiboId(int id){
        return mapper.selectByWeiboId(id);
    }
}
